package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.dto.MessagePageDTO;
import cn.kgc.ssm.entity.Message;
import cn.kgc.ssm.mapper.MessageMapper;
import cn.kgc.ssm.response.ResponseResult;
import cn.kgc.ssm.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 课工场
 * @date 2024/11/19
 * @description
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public ResponseResult messagePage(MessagePageDTO messagePageDTO) {

        PageHelper.startPage(messagePageDTO.getPage(),messagePageDTO.getLimit());
        List<Message>  tableData = messageMapper.selectMessages(messagePageDTO.getMemberName());
        PageInfo<Message> messagePageInfo = new PageInfo<>(tableData);
        long total = messagePageInfo.getTotal();

        return ResponseResult.success().data(tableData).count(total);
    }


    @Transactional
    @Override
    public ResponseResult delete(List<Integer> listIds) {
         messageMapper.deleteMessage(listIds);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult editMessage(Integer id) {

        messageMapper.updateMessage(id);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult publishMessage(Message message) {

        messageMapper.insertMessage(message);

        return ResponseResult.success();
    }
}
